package com.google.code.drift.activity;

import com.google.code.drift.api.AppSkyApp;
import com.google.code.drift.utils.DLog;

import android.app.ActivityGroup;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Message;
import android.view.Window;
import android.widget.Toast;

public abstract class AbstractActivityGroup extends ActivityGroup {

	/**
	 * 全局Application属性
	 */
	public AppSkyApp app;

	private ProgressDialog progressDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		app = (AppSkyApp) getApplication();
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);

		initActionBar();
	}

	/**
	 * 在这里初始化ActionBar里的view们
	 */
	protected abstract void initActionBar();

	@Override
	protected void onDestroy() {
		dismissProgress();
		app = null;
		super.onDestroy();
	}

	@Override
	protected void onTitleChanged(CharSequence title, int color) {
		DLog.i("title改变了，变成：" + title);
	}

	/**
	 * 默认持续时间短
	 * 
	 * @param content
	 */
	public void showToast(String content) {
		Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 自定义持续时间
	 * 
	 * @param resId
	 * @param duration
	 */
	public void showToast(int resId, int duration) {
		Toast.makeText(this, resId, duration).show();
	}

	/**
	 * 显示进度框的内容为Bundle里传递的字符串
	 * 
	 * @param msg
	 */
	public void showProgress(Message msg) {
		Bundle showMessage = msg.getData();
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(this);
		}
		progressDialog.setMessage(showMessage.getString("showMessage"));
		progressDialog.setCancelable(true);
		progressDialog.show();
	}

	/**
	 * 如果进度类不为空，并且还在显示，则dismiss
	 */
	public void dismissProgress() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

}
